﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using A2DFramework.QueueService;
using A2DFramework.InstanceService;
using A2DFramework.EventService.Configuration;
using System.Reflection;
using System.IO;
using System.Xml.Serialization;

namespace A2DFramework.EventService
{
    /// <summary>
    /// 处理事件接收队列中的消息，触发相应事件处理Handler的执行
    /// </summary>
    internal class EventQueueProcessor
    {
        private IQueue queueSrv = ObjectFactory.Resolve<IQueue>();

        public void Process(object state)
        {
            while (true)
            {
                Message<EventInfo> msg = queueSrv.GetRequestFromClient<EventInfo>();

                EventProcessorInfo info = EventProcessorHandlerSearcher.Find(msg.Body.ClassType);

                Type eventClassType = Type.GetType(info.FullClassType);

                Type eventHandlerProcessorType = Type.GetType(info.EventProcessorType);
                MethodInfo mi = eventHandlerProcessorType.GetMethod("Process");
                if (mi == null)
                    continue;

                object evtObj = XmlHelper.Deserialize(msg.Body.Xml, eventClassType);

                object o = Activator.CreateInstance(eventHandlerProcessorType);
                mi.Invoke(o, new object[] { evtObj });
            }
        }
    }
}
